﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace YAMM
{
    /// <summary>
    /// Formular to manage plugins.
    /// </summary>
    public partial class PluginManagerForm : Form
    {
        public PluginManagerForm()
        {
            InitializeComponent();
            Icon = (Icon)global::YAMM.Properties.Resources.ResourceManager.GetObject("ApplicationIcon");
            // TODO only Test
            for (int i = 0; i < PluginManager.PluginList.Count; i++)
            {
                PluginInfo pInfo = new PluginInfo(PluginManager.PluginList[i]);
                pInfo.Width = panel1.Width - 10;
                pInfo.Dock = DockStyle.Top;
                pInfo.DeletePlugin += new EventHandler(DeletePlugin);
                panel1.Controls.Add(pInfo);                
            }            
        }

        private void DeletePlugin(object sender, EventArgs e)
        {
            PluginInfo pInfo = sender as PluginInfo;
            if (pInfo != null)
            {
                //save plugin in job list
                Job job = new Job();
                job.id = JobType.Remove;
                job.guid = pInfo.GUID;
                Jobs.jobs.Add(job);
                Jobs.Save();

                if (MessageBox.Show(
                    "Ein Neustart der Anwendung ist erforderlich, um das PlugIn zu deinstallieren.\nAnwendung neu starten?",
                    "",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question)
                    == DialogResult.Yes)
                    Application.Restart();
                
                //Update GUI
                pInfo.FlagDeleted = true;
                restartButton.Enabled = true;
            }
        }        

        private void installPlugInButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                if (PluginManager.InstallPlugin(dialog.FileName))
                {

                    if (MessageBox.Show(
                        "Ein Neustart der Anwendung ist erforderlich, um das neue PlugIn verfügbar zu machen.\nAnwendung neu starten?",
                        "",
                        MessageBoxButtons.YesNo,
                        MessageBoxIcon.Question)
                        == DialogResult.Yes)
                        Application.Restart();
                    //update GUI
                    restartButton.Enabled = true;
                }
                else
                {
                    MessageBox.Show(
                        "PlugIn konnte nicht installiert werden.\n 1. Prüfen Sie bitte ob es sich um ein gültiges PlugIn handelt.\n 2. Das PlugIn nicht schon installiert ist.",
                        "",
                        MessageBoxButtons.OK,
                        MessageBoxIcon.Error);
                }
            }
        }

        private void restartButton_Click(object sender, EventArgs e)
        {
            Application.Restart();
        }
    }
}
